#include "protocol_analysis.h"
#include "bluetooth_link.h"

void Open_Curtain_Command_Tx(uint8_t percent)
{
    tx_control_t control_tmp;
    uint8_t index;

    control_tmp.control.cmd         = CONTROL_DEVICE_CMD;
    control_tmp.control.key         = CONTROL_OPEN_KEY;
    control_tmp.control.percent     = percent;
    control_tmp.control.reserved0   = 0;
    control_tmp.control.reserved1   = 0;
    control_tmp.control.reserved2   = 0;
    control_tmp.control.reserved3   = 0;
    control_tmp.control.reserved4   = 0;
    control_tmp.control.reserved5   = 0;
    control_tmp.control.reserved6   = 0;
    control_tmp.control.reserved7   = 0;
    control_tmp.control.reserved8   = 0;
    control_tmp.control.reserved9   = 0;
    control_tmp.control.reserved10  = 0;
    control_tmp.control.reserved11  = 0;
    control_tmp.control.reserved12  = 0;
    control_tmp.control.reserved13  = 0;
    control_tmp.control.reserved14  = 0;
    control_tmp.control.reserved15  = 0;
    control_tmp.control.reserved16  = 0;
    index = ble_get_device_index("Curtain_Robot");
    if( (index == 0) || (index == 1) || (index == 2) )
    {
        ble_Send_Buff(index,control_tmp.tx_buff,MAX_BLE_SEND_DATA_LEN);
    }
}

void Close_Curtain_Command_Tx(uint8_t percent)
{
    tx_control_t control_tmp;
    uint8_t index;

    control_tmp.control.cmd         = CONTROL_DEVICE_CMD;
    control_tmp.control.key         = CONTROL_CLOSE_KEY;
    control_tmp.control.percent     = percent;
    control_tmp.control.reserved0   = 0;
    control_tmp.control.reserved1   = 0;
    control_tmp.control.reserved2   = 0;
    control_tmp.control.reserved3   = 0;
    control_tmp.control.reserved4   = 0;
    control_tmp.control.reserved5   = 0;
    control_tmp.control.reserved6   = 0;
    control_tmp.control.reserved7   = 0;
    control_tmp.control.reserved8   = 0;
    control_tmp.control.reserved9   = 0;
    control_tmp.control.reserved10  = 0;
    control_tmp.control.reserved11  = 0;
    control_tmp.control.reserved12  = 0;
    control_tmp.control.reserved13  = 0;
    control_tmp.control.reserved14  = 0;
    control_tmp.control.reserved15  = 0;
    control_tmp.control.reserved16  = 0;
    index = ble_get_device_index("Curtain_Robot");
    if( (index == 0) || (index == 1) || (index == 2) )
    {
        ble_Send_Buff(index,control_tmp.tx_buff,MAX_BLE_SEND_DATA_LEN);
    }
}

void Stop_Curtain_Command_Tx(uint8_t percent)
{
    tx_control_t control_tmp;
    uint8_t index;

    control_tmp.control.cmd         = CONTROL_DEVICE_CMD;
    control_tmp.control.key         = CONTROL_STOP_KEY;
    control_tmp.control.percent     = percent;
    control_tmp.control.reserved0   = 0;
    control_tmp.control.reserved1   = 0;
    control_tmp.control.reserved2   = 0;
    control_tmp.control.reserved3   = 0;
    control_tmp.control.reserved4   = 0;
    control_tmp.control.reserved5   = 0;
    control_tmp.control.reserved6   = 0;
    control_tmp.control.reserved7   = 0;
    control_tmp.control.reserved8   = 0;
    control_tmp.control.reserved9   = 0;
    control_tmp.control.reserved10  = 0;
    control_tmp.control.reserved11  = 0;
    control_tmp.control.reserved12  = 0;
    control_tmp.control.reserved13  = 0;
    control_tmp.control.reserved14  = 0;
    control_tmp.control.reserved15  = 0;
    control_tmp.control.reserved16  = 0;
    index = ble_get_device_index("Curtain_Robot");
    if( (index == 0) || (index == 1) || (index == 2) )
    {
        ble_Send_Buff(index,control_tmp.tx_buff,MAX_BLE_SEND_DATA_LEN);
    }
}

void TurnOnLamp_Command_Tx(uint8_t percent)
{
    tx_control_t control_tmp;
    uint8_t index;

    control_tmp.control.cmd         = CONTROL_DEVICE_CMD;
    control_tmp.control.key         = CONTROL_TURNONLAMP_KEY;
    control_tmp.control.percent     = percent;
    control_tmp.control.reserved0   = 0;
    control_tmp.control.reserved1   = 0;
    control_tmp.control.reserved2   = 0;
    control_tmp.control.reserved3   = 0;
    control_tmp.control.reserved4   = 0;
    control_tmp.control.reserved5   = 0;
    control_tmp.control.reserved6   = 0;
    control_tmp.control.reserved7   = 0;
    control_tmp.control.reserved8   = 0;
    control_tmp.control.reserved9   = 0;
    control_tmp.control.reserved10  = 0;
    control_tmp.control.reserved11  = 0;
    control_tmp.control.reserved12  = 0;
    control_tmp.control.reserved13  = 0;
    control_tmp.control.reserved14  = 0;
    control_tmp.control.reserved15  = 0;
    control_tmp.control.reserved16  = 0;
    index = ble_get_device_index("Table_Lamp");
    if( (index == 0) || (index == 1) || (index == 2) )
    {
        ble_Send_Buff(index,control_tmp.tx_buff,MAX_BLE_SEND_DATA_LEN);
    }
}

void TurnOffLamp_Command_Tx(uint8_t percent)
{
    tx_control_t control_tmp;
    uint8_t index;

    control_tmp.control.cmd         = CONTROL_DEVICE_CMD;
    control_tmp.control.key         = CONTROL_TURNOFFLAMP_KEY;
    control_tmp.control.percent     = percent;
    control_tmp.control.reserved0   = 0;
    control_tmp.control.reserved1   = 0;
    control_tmp.control.reserved2   = 0;
    control_tmp.control.reserved3   = 0;
    control_tmp.control.reserved4   = 0;
    control_tmp.control.reserved5   = 0;
    control_tmp.control.reserved6   = 0;
    control_tmp.control.reserved7   = 0;
    control_tmp.control.reserved8   = 0;
    control_tmp.control.reserved9   = 0;
    control_tmp.control.reserved10  = 0;
    control_tmp.control.reserved11  = 0;
    control_tmp.control.reserved12  = 0;
    control_tmp.control.reserved13  = 0;
    control_tmp.control.reserved14  = 0;
    control_tmp.control.reserved15  = 0;
    control_tmp.control.reserved16  = 0;
    index = ble_get_device_index("Table_Lamp");
    if( (index == 0) || (index == 1) || (index == 2) )
    {
        ble_Send_Buff(index,control_tmp.tx_buff,MAX_BLE_SEND_DATA_LEN);
    }
}

void Set_Time_Command_Tx(clock_time_t* time)
{
    tx_time_t time_tmp;
    time_tmp.time.cmd           = SET_PARA_CMD;
    time_tmp.time.key           = SET_TIME_KEY;
    time_tmp.time.year_low      = (time->year >> 0) & 0xFF;
    time_tmp.time.year_heigh    = (time->year >> 8) & 0xFF;
    time_tmp.time.month         = time->month;
    time_tmp.time.day           = time->day;
    time_tmp.time.hour          = time->hour;
    time_tmp.time.minute        = time->minute;
    time_tmp.time.second        = time->second;
    time_tmp.time.week_day      = time->day_of_week;
    time_tmp.time.reserved0     = 0;
    time_tmp.time.reserved1     = 0;
    time_tmp.time.reserved2     = 0;
    time_tmp.time.reserved3     = 0;
    time_tmp.time.reserved4     = 0;
    time_tmp.time.reserved5     = 0;
    time_tmp.time.reserved6     = 0;
    time_tmp.time.reserved7     = 0;
    time_tmp.time.reserved8     = 0;
    time_tmp.time.reserved9     = 0;
    ble_Send_Buff(0,time_tmp.tx_buff,MAX_BLE_SEND_DATA_LEN);
}

void Enter_Dfu_Mode(void)
{
    ota_cmd_t ota_cmd_tmp;
    ota_cmd_tmp.tx_buff[0] = 1;
    ble_Send_Buff(0,ota_cmd_tmp.tx_buff,MAX_BLE_SEND_DATA_LEN);
}